<template>
  <div class="dialog">
    <el-dialog :title="title" :visible.sync="visible" :close-on-click-modal="false" width="70%" top="10vh" append-to-body>
      <el-form ref="tblMain" :model="tblMain" labelPosition="right" inline="" label-suffix=":" label-width="80px">
        <el-form-item>
          <el-button type="primary" size="small" @click="initTableData()">查询</el-button>
          <el-button size="small" @click="dataReset()">清空</el-button>
          <el-button size="small" type="info" @click="handleAdd()">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="tblMain" :data="tblMain.items" v-loading.body="tblMain.loading" border stripe>
        <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="deleteRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="dictItemName" label="字典名称" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="dictItemCode" label="字典代码" align="center" show-overflow-tooltip></el-table-column>
        <!-- <el-table-column prop="dictItemFcode" label="字典父级代码" align="center" show-overflow-tooltip></el-table-column> -->
        <!-- <el-table-column prop="dictCode" label="字典父级代码" align="center" show-overflow-tooltip></el-table-column> -->
        <el-table-column prop="remarks" label="备注" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="serial" label="排序" width="100" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="status" width="100" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '1'" type="success">启用</el-tag>
            <el-tag v-else-if="scope.row.status == '0'" type="danger">禁用</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="margin-top: 10px" :current-page.sync="tblMain.currentPage" :page-size="tblMain.pageSize"
        :total="tblMain.total" layout="total, sizes, prev, pager, next" @size-change="handleSizeChange"
        @current-change="handleCurrentChange"></el-pagination>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeNetWorker()">关 闭</el-button>
      </div>
      <dictionary-item-detail-dialog ref="itemDetail" @ok="modalFormOk"></dictionary-item-detail-dialog>
    </el-dialog>
  </div>
</template>
<script>
import DictionaryItemDetailDialog from './DictionaryItemDetailDialog'
import {
  dictionaryItemList,
  dictionaryItemDel
} from '@/api/common'
import tool from "@/tool";
export default {
  name: "DictionaryItemDialog",
  components: {
    DictionaryItemDetailDialog
  },
  data() {
    return {
      title: "",
      visible: false,
      curRow: {},
      tblMain: tool.genTableData({
        param: {
          name: "",
        },
        items: [],
      }),
    };
  },
  methods: {
    add(row) {
      this.visible = true;
      this.curRow = row;
      this.title = row.dictName;
      this.initTableData();
    },
    handleAdd() {
      this.$refs.itemDetail.add(this.curRow);
      this.$refs.itemDetail.title = "新增";
    },
    handleEdit(val) {
      this.$refs.itemDetail.edit(val);
      this.$refs.itemDetail.title = "编辑";
    },
    modalFormOk() {
      this.initTableData();
    },
    // 单个删除
    async deleteRow(val) {
      this.$confirm('确定删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = []
        params.push(val.id)
        await this.deleteTblMain(params)
      }).catch(() => { })
    },
    // 删除数据
    deleteTblMain(params) {
      let me = this
      me.tblMain.loading = true
      dictionaryItemDel({ ids: params }).then((res) => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.$message.success('删除成功')
          this.initTableData()
        }
      }).catch(() => {
        me.$message.error('请求失败')
        me.tblMain.loading = false
      })
    },
    closeNetWorker() {
      this.tblMain.items = [];
      this.visible = false;
    },
    handleSizeChange(val) {
      this.tblMain.pageSize = val;
      this.initTableData();
    },
    handleCurrentChange(val) {
      this.tblMain.currentPage = val;
      this.initTableData();
    },
    dataReset() {
      this.tblMain.currentPage = 1;
      this.tblMain.pageSize = 10;
      this.tblMain.param.name = "";
      this.initTableData();
    },
    initTableData() {
      let me = this;
      let param = {
        dictCode: me.curRow.dictCode,
        dictName: me.curRow.dictName,
        page: me.tblMain.currentPage,
        size: me.tblMain.pageSize,
      };
      me.tblMain.loading = true;
      dictionaryItemList(param).then((res) => {
        me.tblMain.loading = false;
        let data = res.data;
        if (data && data.code == 0) {
          me.tblMain.items = data.data.records;
          me.tblMain.total = data.data.total;
        }
      });
    },
  },
};
</script>

<style scoped></style>
